package com.example.base

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.example.base.tools.CacheTool
import com.example.base.tools.ConfigTool
import com.example.base.tools.SystemApiTool
import com.example.base.tools.UIManager
import com.kongzue.dialogx.DialogX
import java.lang.ref.WeakReference

/**
 * App 的基础封装类, 做了一些基础的操作与封装
 */
open  class BaseApp : Application() {

    companion object {
        lateinit var context: Application
        var USER_TOKEN1 = ""

    }
    object ActivityManager {
        // 弱引用
        private var sCurrentActivityWeakRef: WeakReference<Activity>? = null

        fun getCurrentActivity(): Activity? = sCurrentActivityWeakRef?.get()

        fun setCurrentActivity(activity: Activity) {
            sCurrentActivityWeakRef = WeakReference(activity)
        }
    }
    override fun onCreate() {
        super.onCreate()

        context = this

        registerActivityLifecycleCallbacks(UIManager())

        ConfigTool.init(context)
        CacheTool.init(context)
        SystemApiTool.init(context)

        DialogX.init(this)
        USER_TOKEN1 = ConfigTool.getString("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxNDg1NCIsImV4cCI6MTY5MjE1MzgxOX0.QObOvSc1SMNIaAFq9dz_h7W0eej7R38dM4GUqhXx57s")


// 监听所有Activity的生命周期回调
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityResumed(activity: Activity) {
                // 在此处设置当前的Activity
                ActivityManager.setCurrentActivity(activity)
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
            }
        })

    }

}